﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
    public class CriteriaPropertyRentalViewModel : BaseViewModel
    {
        private SearchResidentialCriteria _criteria;
        public SearchResidentialCriteria Criteria
        {
            get
            {
                if (_criteria == null)
                {
                    _criteria = new SearchResidentialCriteria();
                    UpdateFields();
                }
                return _criteria;
            }
            set
            {
                _criteria = value;
                UpdateFields();
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private Locality _selectedRegion;
        public Locality SelectedRegion
        {
            get { return _selectedRegion; }
            set
            {
                _selectedRegion = value;
                NotifyPropertyChanged("RegionName");
                SelectedDistrict = null;
                Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
            }
        }

        public string RegionName
        {
            get
            {
                return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
            }
        }

        public List<SelectorItem<Locality>> Regions
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Locality>>();
                list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
                list.AddRange(
                    App.TradeMe.Catalog.Localities.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private District _selectedDistrict;
        public District SelectedDistrict
        {
            get { return _selectedDistrict; }
            set
            {
                _selectedDistrict = value;
                NotifyPropertyChanged("DistrictName");
                NotifyPropertyChanged("DistrictVisible");
                SelectedSuburb = null;
                Criteria.District = _selectedDistrict != null ? _selectedDistrict.DistrictId : 0;
            }
        }


        public bool DistrictVisible
        {
            get
            {
                if (SelectedRegion == null) return false;
                if (SelectedRegion.Districts == null) return false;
                return SelectedRegion.Districts.Length > 0;
            }
        }

        public string DistrictName
        {
            get
            {
                return SelectedDistrict == null ? "All districts" : SelectedDistrict.Name;
            }
        }

        public List<SelectorItem<District>> Districts
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!DistrictVisible) return null;
                var list = new List<SelectorItem<District>>();
                list.Add(new SelectorItem<District> { Title = "All districts" });
                list.AddRange(
                    SelectedRegion.Districts.Select(
                        district => new SelectorItem<District> { Title = district.Name, Item = district }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private Suburb _selectedSuburb;
        public Suburb SelectedSuburb
        {
            get { return _selectedSuburb; }
            set
            {
                _selectedSuburb = value;
                NotifyPropertyChanged("SuburbName");
                NotifyPropertyChanged("SuburbVisible");
                NotifyPropertyChanged("SearchNearbyVisible");
                Criteria.Suburb = _selectedSuburb != null ? _selectedSuburb.SuburbId : 0;
            }
        }


        public bool SuburbVisible
        {
            get
            {
                if (SelectedDistrict == null) return false;
                if (SelectedDistrict.Suburbs == null) return false;
                return SelectedDistrict.Suburbs.Length > 0;
            }
        }

        public bool SearchNearbyVisible
        {
            get { return SelectedSuburb != null; }
        }

        public string SuburbName
        {
            get
            {
                return SelectedSuburb == null ? "All suburbs" : SelectedSuburb.Name;
            }
        }

        public List<SelectorItem<Suburb>> Suburbs
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!SuburbVisible) return null;
                var list = new List<SelectorItem<Suburb>>();
                list.Add(new SelectorItem<Suburb> { Title = "All suburbs" });
                list.AddRange(
                    SelectedDistrict.Suburbs.Select(
                        suburb => new SelectorItem<Suburb> { Title = suburb.Name, Item = suburb }));
                return list.ToList();
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedPropertyType;
        public SelectorItem<string> SelectedPropertyType
        {
            get
            {
                return _selectedPropertyType ?? (_selectedPropertyType = new SelectorItem<string> { Title = "Any property type" });
            }
            set
            {
                _selectedPropertyType = value;
                NotifyPropertyChanged("PropertyTypeName");
                Criteria.PropertyType = _selectedPropertyType != null ? _selectedPropertyType.Item : null;
            }
        }
        public string PropertyTypeName
        {
            get { return SelectedPropertyType.Title; }
            set { SelectedPropertyType.Title = value; }
        }
        public string PropertyTypeItem
        {
            get { return Criteria.PropertyType; }
            set { Criteria.PropertyType = value; }
        }

        public List<SelectorItem<string>> PropertyTypeItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Any property type", Item = "Any"},
                        new SelectorItem<string> {Title = "Apartment", Item = "Apartment"},
                        new SelectorItem<string> {Title = "House", Item = "House"},
                        new SelectorItem<string> {Title = "Section", Item = "Section"},
                        new SelectorItem<string> {Title = "Townhouse", Item = "Townhouse"},
                        new SelectorItem<string> {Title = "Unit", Item = "Unit"},
                        new SelectorItem<string> {Title = "Other", Item = "Other"},
                        new SelectorItem<string> {Title = "Villa", Item = "Villa"},
                    };
                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedPriceFrom;
        public SelectorItem<string> SelectedPriceFrom
        {
            get
            {
                return _selectedPriceFrom ?? (_selectedPriceFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceFrom = value;
                NotifyPropertyChanged("PriceFromName");
                Criteria.MinPrice = _selectedPriceFrom.Item != null ? Convert.ToInt32(_selectedPriceFrom.Item) : 0;
            }
        }
        public string PriceFromName
        {
            get { return SelectedPriceFrom.Title; }
            set { SelectedPriceFrom.Title = value; }
        }

        private SelectorItem<string> _selectedPriceTo;
        public SelectorItem<string> SelectedPriceTo
        {
            get
            {
                return _selectedPriceTo ?? (_selectedPriceTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPriceTo = value;
                NotifyPropertyChanged("PriceToName");
                Criteria.MaxPrice = _selectedPriceTo != null ? Convert.ToInt32(_selectedPriceTo.Item) : 0;
            }
        }
        public string PriceToName
        {
            get { return SelectedPriceTo.Title; }
            set { SelectedPriceTo.Title = value; }
        }

        public List<SelectorItem<string>> PriceItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="50", Title = "$50"},
                        new SelectorItem<string> {Item="100", Title = "$100"},
                        new SelectorItem<string> {Item="150", Title = "$150"},
                        new SelectorItem<string> {Item="200", Title = "$200"},
                        new SelectorItem<string> {Item="250", Title = "$250"},
                        new SelectorItem<string> {Item="300", Title = "$300"},
                        new SelectorItem<string> {Item="350", Title = "$350"},
                        new SelectorItem<string> {Item="400", Title = "$400"},
                        new SelectorItem<string> {Item="500", Title = "$500"},
                        new SelectorItem<string> {Item="600", Title = "$600"},
                        new SelectorItem<string> {Item="800", Title = "$800"},
                        new SelectorItem<string> {Item="1000", Title = "$1000+"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedBedroomsFrom;
        public SelectorItem<string> SelectedBedroomsFrom
        {
            get
            {
                return _selectedBedroomsFrom ?? (_selectedBedroomsFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedBedroomsFrom = value;
                NotifyPropertyChanged("BedroomsFromName");
                Criteria.MinBedrooms = _selectedBedroomsFrom.Item != null ? Convert.ToInt32(_selectedBedroomsFrom.Item) : 0;
            }
        }
        public string BedroomsFromName
        {
            get { return SelectedBedroomsFrom.Title; }
            set { SelectedBedroomsFrom.Title = value; }
        }

        private SelectorItem<string> _selectedBedroomsTo;
        public SelectorItem<string> SelectedBedroomsTo
        {
            get
            {
                return _selectedBedroomsTo ?? (_selectedBedroomsTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedBedroomsTo = value;
                NotifyPropertyChanged("BedroomsToName");
                Criteria.MaxBedrooms = _selectedBedroomsTo != null ? Convert.ToInt32(_selectedBedroomsTo.Item) : 0;
            }
        }
        public string BedroomsToName
        {
            get { return SelectedBedroomsTo.Title; }
            set { SelectedBedroomsTo.Title = value; }
        }

        public List<SelectorItem<string>> BedroomItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="1", Title = "1"},
                        new SelectorItem<string> {Item="2", Title = "2"},
                        new SelectorItem<string> {Item="3", Title = "3"},
                        new SelectorItem<string> {Item="4", Title = "4"},
                        new SelectorItem<string> {Item="5", Title = "5"},
                        new SelectorItem<string> {Item="6", Title = "6+"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedBathroomsFrom;
        public SelectorItem<string> SelectedBathroomsFrom
        {
            get
            {
                return _selectedBathroomsFrom ?? (_selectedBathroomsFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedBathroomsFrom = value;
                NotifyPropertyChanged("BathroomsFromName");
                Criteria.MinBathrooms = _selectedBathroomsFrom.Item != null ? Convert.ToInt32(_selectedBathroomsFrom.Item) : 0;
            }
        }
        public string BathroomsFromName
        {
            get { return SelectedBathroomsFrom.Title; }
            set { SelectedBathroomsFrom.Title = value; }
        }

        private SelectorItem<string> _selectedBathroomsTo;
        public SelectorItem<string> SelectedBathroomsTo
        {
            get
            {
                return _selectedBathroomsTo ?? (_selectedBathroomsTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedBathroomsTo = value;
                NotifyPropertyChanged("BathroomsToName");
                Criteria.MaxBathrooms = _selectedBathroomsTo != null ? Convert.ToInt32(_selectedBathroomsTo.Item) : 0;
            }
        }
        public string BathroomsToName
        {
            get { return SelectedBathroomsTo.Title; }
            set { SelectedBathroomsTo.Title = value; }
        }

        public List<SelectorItem<string>> BathroomItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="1", Title = "1"},
                        new SelectorItem<string> {Item="2", Title = "2"},
                        new SelectorItem<string> {Item="3", Title = "3"},
                        new SelectorItem<string> {Item="4", Title = "4"},
                        new SelectorItem<string> {Item="5", Title = "5"},
                        new SelectorItem<string> {Item="6", Title = "6+"},
                    };

                return list;
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedSortBy;
        public SelectorItem<string> SelectedSortBy
        {
            get
            {
                return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
            }
            set
            {
                _selectedSortBy = value;
                NotifyPropertyChanged("SortByName");
                Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
            }
        }
        public string SortByName
        {
            get { return SelectedSortBy.Title; }
            set { SelectedSortBy.Title = value; }
        }

        public string SortByItem
        {
            get { return Criteria.SortOrder; }
            set { Criteria.SortOrder = value; }
        }

        public List<SelectorItem<string>> SortByItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Default", Item = "Default"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        public void UpdateFields()
        {
            int district = Criteria.District;
            int suburb = Criteria.Suburb;
            if (Criteria.Region != 0)
            {
                SelectedRegion = App.TradeMe.Catalog.Localities.FirstOrDefault(l => l.LocalityId == Criteria.Region);
                if (district != 0)
                {
                    SelectedDistrict = SelectedRegion.Districts.FirstOrDefault(d => d.DistrictId == district);
                    if (suburb != 0)
                        SelectedSuburb = SelectedDistrict.Suburbs.FirstOrDefault(s => s.SuburbId == suburb);
                }
            }
            if (Criteria.MinBedrooms > 0)
                SelectedBedroomsFrom = BedroomItems.FirstOrDefault(b => b.Item == Criteria.MinBedrooms.ToString());
            if (Criteria.MaxBedrooms > 0)
                SelectedBedroomsTo = BedroomItems.FirstOrDefault(b => b.Item == Criteria.MaxBedrooms.ToString());

            if (Criteria.MinBathrooms > 0)
                SelectedBathroomsFrom = BathroomItems.FirstOrDefault(b => b.Item == Criteria.MinBathrooms.ToString());
            if (Criteria.MaxBathrooms > 0)
                SelectedBathroomsTo = BathroomItems.FirstOrDefault(b => b.Item == Criteria.MaxBathrooms.ToString());

            if (Criteria.PropertyType != null)
                SelectedPropertyType = PropertyTypeItems.FirstOrDefault(t => t.Item == Criteria.PropertyType);

            if (!string.IsNullOrEmpty(Criteria.SortOrder))
                SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);
        }

        public void Reset()
        {
            SelectedRegion = null;
            SelectedPriceFrom = null;
            SelectedPriceTo = null;
            SelectedBedroomsFrom = null;
            SelectedBedroomsTo = null;
            SelectedBathroomsFrom = null;
            SelectedBathroomsTo = null;
            SelectedSortBy = null;
            SelectedPropertyType = null;
            UpdateFields();
        }

    }
}
